package com.Shoping.controller;

import com.Shoping.entity.Address;
import com.Shoping.entity.User;
import com.Shoping.service.AddressService;
import com.Shoping.service.impl.AddressServiceImpl;
import com.alibaba.fastjson.JSON;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.List;

@WebServlet(value = "/address")
public class AddressController extends BaseServlet {
    public String addAddress(HttpServletRequest request, HttpServletResponse response) throws SQLException {
        String aName = request.getParameter("aName");
        String aAddress = request.getParameter("aAddress");
        String aPhone = request.getParameter("aPhone");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Integer uId = user.getuId();
        Address address = new Address();
        address.setaName(aName);
        address.setaPhone(aPhone);
        address.setaAddress(aAddress);
        address.setuId(uId);
        AddressService addressService = new AddressServiceImpl();
        Integer k = addressService.addAddress(address);
        if(k!=0){
            return "1";
        }
        return null;
    }

    public String AddressAll(HttpServletRequest request,HttpServletResponse response) throws SQLException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        Integer uId = user.getuId();
        AddressService addressService = new AddressServiceImpl();
        List<Address> list = addressService.selectByUid(uId);
        return JSON.toJSONString(list);
    }
}
